<?php
defined('IN_MAIN') or exit('Invalid Access!');

function check_sufficient_stockist_qty($sid, $prods){
/*
This function check every input product $prods (normal product / products within a jump start package) and check against the input stockist id
to see if the stockist available qty is sufficient
input $prods must have the format
$prods[index][refno]
$prods[index][qty]
if product is not exist, then it return the product that is not exist
if no product is provided, it wil return true
if no stockist id provided / stockist id not found, then it will return false
if all qty are sufficient, then it will return boolean true, otherwise it will return the product that the qty is insufficient
*/

	global $vars;
	$db=$vars["db"];

	if(!$sid || !stockist_id_found($sid)){
		$error = true;
	}else{
		//below to format the requested qty & available qty for each of the products in $prods into $chk_prod & $prod_avai_qty respectively
		//and will expand the products inside a package into the $chk_prod & $prod_qty too
		for($i=0,$t=count($prods);$i<$t;$i++){
			//check if stockist has the stock
			$this_prod=@mysql_fetch_assoc(mysql_query("select qty from $db->stockist_inventory where sid='$sid' and refno='".$prods[$i]["refno"]."'")) or false;
			if(!$this_prod){
				//if stockist don't have this stock check to see it's a package (stockist will not have the package, but will have the sub-products of that package)
				$this_prod_sys=@mysql_fetch_assoc(mysql_query("select id, prod_type, qty from $db->products where refno='".$prods[$i]["refno"]."'")) or false;
				if($this_prod_sys["prod_type"]=="p"){
					$this_prod=$this_prod_sys;
				}else{
					$prod_not_exist[StripSlashes($prods[$i]["refno"])]=1;
				}
			}
			if($this_prod){
				if($this_prod["prod_type"]=='p'){
					$t2=@mysql_num_rows($r2=mysql_query("select refno, qty as req_qty from $db->package_detail where pkid='".$this_prod["id"]."'"));
					for($j=0;$j<$t2;$j++){
						$this_js=mysql_fetch_assoc($r2);
						$this_js_prod=@mysql_fetch_assoc($r3=mysql_query($s="select qty from $db->stockist_inventory where sid='$sid' and refno='".mysql_real_escape_string($this_js["refno"])."'")) or false;
						if(!$this_js_prod){
							$js_prod_not_exist[StripSlashes($prods[$i]["refno"])][$this_js["refno"]]=1;
						}else{
							$prod_avai_qty[$this_js["refno"]]=$this_js_prod["qty"];
							$chk_prod[$this_js["refno"]]+=$this_js["req_qty"]*$prods[$i]["qty"];
							$js_prod_detail[StripSlashes($prods[$i]["refno"])][$j]=$this_js["refno"];
						}
					}
				}else{
					$prod_avai_qty[StripSlashes($prods[$i]["refno"])]=$this_prod["qty"];
					$chk_prod[StripSlashes($prods[$i]["refno"])]+=$prods[$i]["qty"];
				}
			}
		}
		//check if product exist
		if(count($prod_not_exist)){
			foreach($prod_not_exist as $refno => $no){
				$prod_not_exist_err.=replace_tag(__("Product with reference no. '<%refno%>' is not available from this stockist."), array("<%refno%>"=>strval($refno)))."<br />\n";
			}
		}
		if(count($js_prod_not_exist)){
			foreach($js_prod_not_exist as $js_refno => $detail){
				foreach($detail as $refno => $no){
					$prod_not_exist_err.=replace_tag(__("Product with reference no. '<%refno%>' in the package with reference no. '<%jsrefno%>' is not available from this stockist."), array("<%refno%>"=>strval($refno), "<%jsrefno%>"=>strval($js_refno)))."<br />\n";
				}
			}
		}
		//check if product quantity is sufficient
		if(!count($prod_not_exist) && !count($js_prod_not_exist) && count($chk_prod)){
			foreach($chk_prod as $refno => $req_qty){
				if($req_qty > $prod_avai_qty[$refno]){
					$js_prod_ins=false;
					//below check to see if the current product belongs to the products of a jumpstart package
					if(count($js_prod_detail)){
						//below will check every jumpstart package for the same product and every jump start package will appear in the error msg in the jump start package has the product
						foreach($js_prod_detail as $js_refno => $details){
							for($i=0,$t=count($details);$i<$t;$i++){
								if($details[$i] == $refno){
									$ins_qty.=replace_tag(__("The stockist does not have sufficient quantity for product with reference no. '<%refno%>' in the package with reference no. '<%js_refno%>'."), array("<%refno%>"=>strval($refno), "<%js_refno%>"=>strval($js_refno)))."<br />\n";
									$i=$t;
									$js_prod_ins=true;
								}
							}
						}
					}
					//if insufficient product is not in the jumpstart package (means in the individual product)
					if(!$js_prod_ins){
						$ins_qty.=replace_tag(__("The stockist does not have sufficient quantity for product with reference no. '<%refno%>'."), array("<%refno%>"=>strval($refno)))." ".__("Required:")." $req_qty ".__("Available:")." $prod_avai_qty[$refno]"."<br />\n";
					}
				}
			}
		}
	}

	$return = $prod_not_exist_err? $prod_not_exist_err : ($error? false : ($ins_qty? $ins_qty : true));
	return $return;
}
?>